package com.jch.dm6_active_object.example;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;

public class MMSDeliveryServlet extends HttpServlet {

    private static final Logger log = LogManager.getLogger(MMSDeliveryServlet.class);

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MMSDeliverRequest mmsDeliverRequest = this.parseRequest(req.getInputStream());
        Recipient shortNumberRecipient = mmsDeliverRequest.getRecipient();
        Recipient originalNumberRecipient = null;
        try {
            // 接收短号转为长号
            originalNumberRecipient = convertShortNumber(shortNumberRecipient);
        } catch (SQLException e) {
            // 接收方短号转换为长号时发生数据库异常，触发请求消息的缓存
            AsyncRequestPersistence.getInstance().store(mmsDeliverRequest);
            // 省略其他代码
            resp.setStatus(202);
        }
        log.info(String.valueOf(originalNumberRecipient));
    }

    private MMSDeliverRequest parseRequest(InputStream reqInputStream){
        MMSDeliverRequest mmsDeliverReq = new MMSDeliverRequest();
        // 省略其他代码
        return mmsDeliverReq;
    }

    private Recipient convertShortNumber(Recipient shortNumberRecipient) throws SQLException{
        Recipient recipient = null; // 省略其他代码
        return recipient;
    }
}
